package com.sheng.project.demo.process;

import com.sheng.project.demo.utils.Sm2Util;
import com.sheng.project.demo.utils.Sm3Util;
import com.sheng.project.demo.utils.Sm4Util;
import org.bouncycastle.jcajce.provider.asymmetric.ec.BCECPrivateKey;
import org.bouncycastle.util.encoders.Hex;

import java.math.BigInteger;
import java.util.HashMap;

public class Decrypt {

    /**
     * d:三方通过generateKeyPair生成的
     * hexCipherText：中行加密后的数据
     * return：解密后的原文
     */
    public static String decryptBySm2(BigInteger d, String hexCipherText) {
        BCECPrivateKey privatekey = Sm2Util.getPrivatekeyFromD(d);
        String cipher = "04" + hexCipherText;
        byte[] bytes = Sm2Util.sm2Decrypt(Hex.decode(cipher), privatekey);
        return new String(bytes);
    }

    public static void main(String[] args) throws Exception {
        String body = "205D7D3AAE14F61803D2FDD55C695AA6F1307F812701C1F37461F57D404D8CB4EFE821BF80BDDF6F244E53EA5D7186EAEF82685E4600201F7AF90218939374F5F61BD42C20E29F8AD245F06CFDF42347F4617C88E0D9FF3FA7EFA8BC76917C01398AB3291080CBC799DF36A782CAD9C7F0F76FA33545D82AAB138E7F6BFD54D3FA43730DCB2DF58EC0CD2290C524A47918C707CE31CBB33CD04745420563E410B2CCD86F3844E82E6D896BA36BC69D9EEA0FFA0CB4A887C28ACEB15287CE17066ECE301E731C4B0829CBADF7070FB6B9A39879779D7EC30FAF6BEC913C6895C43E2EBFEE8B21116B72DE8DB8C774C978F18F608DE443C9E01EED699656DD5810B523AA636E9B6DE3F4052463DAFC909A169BA65354694D04EF09CDAD0B82F04856654A48EBB5C2BA1D3C0B41ACAEFC4D32B5ACF0D04202417F9E97AF1D221CFAEE910A09221640E902129CAEB3E6963EFCEC40245BF6171EC93730B100C13F1C8DAA6E837A05A9449E66918B67B8116D3E4CEFF2BB304BA8C37AB6AF6453AEB4DC67D1A83FA2A777474398CB8A343A50B2834EDBA31B390AB62494D05E0E3BD7798C0469AF551DAA7E688E6BCDAF4FBE83727A6F11BB8708F105C6B8E614A4F6";
        String signature = "C1011DE1CF79D4B48F7B4AA04827781C2ED967BB544310962B71503B67120407";
        String skey = "AA33A1B8627E4FCAF1ECD144E26F56F3AC8EE1D2813D0A659761C6312767E2B052E6F0CACBF9AD1F912765EC0106EC2F812E5C9C771E3EB3F582089C69A8F428FAAEFDD09D9157EA6EB4A8C113EE0BE2D8557F3702EA940BC2F7259D48E3E1E4BA13556A870E7C9DA4DC284457122E8F1BCBD28ED0EDEEE199AF1D20CC0CC2B9";
        HashMap<String, String> process = new Decrypt().process(body, skey, signature);
        System.out.println(process);
    }

    /**
     * 解密过程
     * 1.sm2解密sm4的key
     * 2.sm3验证数据完整性
     * 3.sm4解原数据
     */
    public HashMap<String, String> process(String body, String skey, String signature) throws Exception {
        HashMap<String, String> res = null;
        try {
            String d = "101959616850875943932697770166287063999683974043242337818732170373234155890783";
            String sm4Key = decryptBySm2(new BigInteger(d), skey);
            String plainText = Sm4Util.sm4Decrypt(sm4Key, body);
            boolean tamped = Sm3Util.verify(signature, plainText);
            System.out.println("验签结果：" + tamped);
            if (!tamped) {
                throw new RuntimeException();
            }
            res = new HashMap<>();
            res.put("plainText", plainText);
            return res;
        } catch (Exception e) {
            System.out.println("解密失败");
            e.printStackTrace();
        }
        return res;
    }


}
